<?php

if (file_exists($common_data_server."prebuild/syswords.php"))
    include_once($common_data_server."prebuild/syswords.php");

$isModule = true;
$modLang_id = "";

// Функция clean_name возвращает "чистое" имя переменной, без префиксов
// $k - имя с префиксами
function clean_name($k){
    global $javascript_prefixes, $javascript_prefixes2;
    return preg_replace("/^(_nonempty_|_email_|_date_|_time_|_datetime_|_number_|_select_|_radio_|_alphastring_|_login_|_dirname_|_nonemptyalt_|_maxlen[\d]*_|_minlen[\d]*_)+/", "", $k);
}

foreach($_GET as $key => $value){
	preg_match("/^([\w-]+?)(\d+)$/", $key, $matches);
	$q_params[$matches[2]][clean_name($matches[1])] = $value;
}

foreach($_POST as $key => $value){
    preg_match("/^([\w-]+?)(\d+)$/", $key, $matches);
	$q_params[$matches[2]][clean_name($matches[1])] = $value;
}

// Сгенерить новую куери стринг
function make_query_string($new_params){

	global $q_params;
	$new_query = $_SERVER["QUERY_STRING"];;
	foreach($new_params as $k=>$v){
		$new_query=preg_replace("/(^|&)".$k."=[^&=\$]*/","",$new_query);
	   	if($v!=""){
			if($new_query!="")$amp="&";
			$new_query.=$amp.$k."=".$v;
        }
    }
	$new_query=preg_replace("/\\\['\"]/s","",$new_query);
	return $new_query;
}



class module{

	var $site_id;
	var $page_id;
	var $block_id;
	var $tarea_id;
	var $view_param;
	var $q_param;
	var $version;
	var $lang_id;

	var $title;
	var $body;
	var $keywords;
	var $description;
	var $link;

	var $template_path;

	function get_hash_code(){
		if($this->version==1)return 0;
	    $all_params=$this->block_id.$this->lang_id;
	    foreach($this->view_param as $k=>$vp)
	       	$all_params.=$k.$vp;
	    if(is_array($this->q_param))
	        foreach($this->q_param as $k=>$vp)
	   	    	$all_params.=$k.$vp;
		return md5($all_params.$_GET["print"].$_POST["print"]);
    }

    function init($site_id,$page_id,$block_id,$tarea_id,$view_param,$version,$lang_id,$cache_time,$is_print){
	    global $q_params,$common_data_server, $modLang_id;
	    $this->site_id=$site_id;
	    $this->page_id=$page_id;
	    $this->block_id=$block_id;
	    $this->tarea_id=$tarea_id;
	    $this->view_param=$view_param;
	    $this->q_param=$q_params[$tarea_id];
	    $this->version=$version;
		$this->lang_id=$lang_id;
	    $this->cache_time=$cache_time;
	    $this->is_print=$is_print;
	    $this->separator="\x00"; // разделитель того, что помещается в кэш
		$this->template_path = $common_data_server.'module_templates/'.get_class($this).'/'.$this->view_param['template'].'/';

		$modLang_id = $lang_id;

		$cache_name=$this->get_hash_code();

		$filemtime=@filemtime($common_data_server."block_cache/block".$this->block_id."/".$cache_name.".html");
	    if(!$filemtime || time()-$filemtime>$this->cache_time || $cache_name=="0"){
	        $this->content_init();
	        if($this->cache_time!=0 && $cache_name!="0"){
    	        $this->make_cache($cache_name.".html");
            }
        }else{
            $this->get_cache($cache_name.".html");
        }
    }

    function make_cache($filename){
	    global $common_data_server;
		$content=$this->body.$this->separator.$this->title.$this->separator.$this->keywords.$this->separator.$this->description.$this->separator.$this->link;
	    @mkdir($common_data_server."block_cache/block".$this->block_id."/");
	    chmod($common_data_server."block_cache/block".$this->block_id."/",0777);
		$file=fopen($common_data_server."block_cache/block".$this->block_id."/".$filename,"w");
	    fwrite($file,$content);
	    fclose($file);
		chmod($common_data_server."block_cache/block".$this->block_id."/".$filename,0777);
	}

	function get_cache($filename){
	    global $common_data_server;
		$content=join("",file($common_data_server."block_cache/block".$this->block_id."/".$filename));
	    $deser_cache=explode($this->separator,$content);
	    $this->body=$deser_cache[0];
	    $this->title=$deser_cache[1];
	    $this->keywords=$deser_cache[2];
	    $this->description=$deser_cache[3];
	    $this->link=$deser_cache[4];
    }

	function content_init(){
	}

	function show(){
		return $this->body;
    }

	function get_title(){
	    if($this->title!="")
	        return " :: ".$this->title;
    }

	function get_keywords(){
	    return $this->keywords;
    }

	function get_description(){
	    return $this->description;
    }

	function get_link(){
	    return $this->link;
    }

	/***************************** getTAreaId *******************/
	function getTAreaId($page_id){
	/*  Назначение: возвращает tarea_id (главной области) для page_id.  */
	    if ( $page_id ){
	    	$tarea_arr = sql_select("select
	    								PA.TEMPLATE_AREA_ID as TAREA_ID
	    							from
	    								PAGE_AREA PA,
	    								TEMPLATE_AREA TA
	    							where
	    								PA.PAGE_ID = " . $page_id . " and
	    								PA.VERSION = " . $this->version . " and
	    								PA.TEMPLATE_AREA_ID = TA.TEMPLATE_AREA_ID and
	    								TA.IS_MAIN = 1");
        	$this->tarea_id = $tarea_arr[0]['TAREA_ID'];
        }
    return $this->tarea_id;
	}

	/***************************** go_to_page *******************/
	function go_to_page($page_id) {
	/*
        Назначение: используется для формирования ссылки (перехода) в модуле на другой раздел, содержащий
    			этот модуль в "главной области" странице и указаный в back-end'e администратором
    */
        if ( $page_id ){
			$page_row = sql_select("SELECT PAGE_AREA.TEMPLATE_AREA_ID,PAGE.PARENT_ID,PAGE.DIR_NAME FROM PAGE,PAGE_AREA WHERE PAGE_AREA.PAGE_ID=" . $page_id . " AND PAGE_AREA.PAGE_ID=PAGE.PAGE_ID AND PAGE_AREA.VERSION=PAGE.VERSION and PAGE_AREA.VERSION=" . $this->version);
			$parent = $page_row[0]["PARENT_ID"];
			$redir_path = $page_row[0]["DIR_NAME"]."/";
			while ( $parent != 0 ){
				$parent_rows = sql_select("SELECT PARENT_ID,DIR_NAME FROM PAGE WHERE PAGE_ID=" . $parent . " and PAGE.VERSION=" . $this->version );
				$redir_path = $parent_rows[0]["DIR_NAME"] . "/" . $redir_path;
				$parent = $parent_rows[0]["PARENT_ID"];
		    }
			$redir_path = "/".$redir_path;
		}
		return $redir_path . "index.php";
	}

}
?>